package vip.george.p0682;

import java.util.ArrayList;

public class P682 {
    public int calPoints(String[] operations) {
        ArrayList<Integer> list = new ArrayList<>();
        for (String operation : operations) {
            switch (operation) {
                case "+":
                    int addValue = list.get(list.size() - 1) + list.get(list.size() - 2);
                    list.add(addValue);
                    break;
                case "D":
                    int doubleValue = list.get(list.size() - 1) * 2;
                    list.add(doubleValue);
                    break;
                case "C":
                    list.remove(list.size() - 1);
                    break;
                default:
                    list.add(Integer.parseInt(operation));
            }
        }
        return list.stream().mapToInt(Integer::intValue).sum();
    }

    public static void main(String[] args) {
        P682 p682 = new P682();
        System.out.println(p682.calPoints(new String[]{"5", "2", "C", "D", "+"}));
        System.out.println(p682.calPoints(new String[]{"5", "-2", "4", "C", "D", "9", "+", "+"}));
        System.out.println(p682.calPoints(new String[]{"1"}));
    }
}
